﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Aribaccio;
using Aribaccio.Core;
using Aribaccio.Copyright;
using Aribaccio.Datum;
using Aribaccio.Logger;
using Aribaccio.Security;
using BDPAutodetect.Device;
using BDPAutodetect.DataService;
using BDPAutodetect.Hardware;
using BDPAutodetect.Mgrcore;

namespace BDPAutodetect.Client
{
    /// <summary>
    /// 程序启动类
    /// </summary>
    public class AppStartup : Singleton<AppStartup>
    {
        /// <summary>
        /// 启动容器并注册服务
        /// </summary>
        public void StartupServices()
        {
            //先构建容器配置
            ContainerManager.Instance.BuildConfig(Path.Combine("Config", "Settings.json"));
            //再向容器注册服务
            this.ConfigureServices();
        }

        /// <summary>
        /// 初始化服务
        /// </summary>
        public void StartupInitialize()
        {
            //从容器中获取-日志管理初始化
            var loggerLeader = ContainerManager.Instance.GetContainerServiceRequired<LoggerLeader>();
            loggerLeader?.Initialize();
            //从容器中获取-初始化系统配置
            var systemManager = ContainerManager.Instance.GetContainerServiceRequired<SettingSystemManager>();
            systemManager?.Initialize();
        }

        /// <summary>
        /// 
        /// </summary>
        public void StartupSysSetting()
        { 
            
        }

        /// <summary>
        /// 向容器注册应用服务
        /// </summary>
        protected void ConfigureServices()
        {
            if (ContainerManager.Instance == null) return;
            //容器服务配置对象
            IConfiguration configuration = ContainerManager.Instance.Configuration;
            //容器服务注册对象
            IServiceCollection service = ContainerManager.Instance.ServiceCollection;

            // 0-注册Logger实例
            service?.AddSingleton<LoggerLeader>();

            // 1-全局容器中注册数据库连接配置
            //service.Configure<CoreConnectionOption>(configuration.GetSection(nameof(CoreConnectionOption)));
            service?.Configure<CoreConnectionOption>(options => configuration?.GetSection("ConnectionStrings").Bind(options));

            // 2-全局容器中注册自定义服务
            service?.AddDbContext<CoreDBContext>();
            service?.AddTransient<ICoreDBContext, CoreDBContext>();
            service?.AddTransient<ICoreDBContextFactory, CoreDBContextFactory>();
            service?.AddTransient<ISecurity, SecurityAES>();
            service?.AddTransient<IRegisterCopyLimits, RegisterCopyLimits>();
            service?.AddSingleton<IDataServiceBasic, DataServiceBasic>();
            service?.AddSingleton<IDataServiceDetect, DataServiceDetect>();
            
            // 3-向全局容器中注册管理中心[Manager]
            service?.AddSingleton<BarcodeHookEx>();
            service?.AddSingleton<SettingSystemManager>();
            service?.AddSingleton<DeviceManager>();
            service?.AddSingleton<DeviceParaManager>();
            service?.AddSingleton<StorageDeviceManager>();
            service?.AddSingleton<RolePowerManager>();
            service?.AddSingleton<UserManager>();
            service?.AddSingleton<SlaveManager>();
            service?.AddSingleton<StationManager>();
            service?.AddSingleton<TargetSlaveManager>();
            service?.AddSingleton<ParameterManager>();
            service?.AddSingleton<DetectDeviceManager>();
            service?.AddSingleton<DetectRecordManager>();
            service?.AddSingleton<DetectJobManager>();

            // 4-向全局容器中注册视图模型[ViewModels]
            service?.AddSingleton<AppLoader>();
            service?.AddTransient<LoginWindowModel>();
            service?.AddSingleton<MainWindowModel>();
            service?.AddSingleton<SettingStationViewModel>();
            service?.AddSingleton<SettingTargetViewModel>();
            service?.AddSingleton<SettingDeviceSlaveViewModel>();
            service?.AddSingleton<SettingDeviceChannelViewModel>();
            service?.AddSingleton<SettingUserRoleViewModel>();
            service?.AddSingleton<SettingRolePowerViewModel>();
            service?.AddSingleton<SettingSystemViewModel>();
            service?.AddSingleton<SettingViewModel>();
            service?.AddSingleton<ParameterViewModel>();
            service?.AddSingleton<DetectTargetViewModel>();
            service?.AddSingleton<DetectRecordViewModel>();
            service?.AddSingleton<MainPageViewModel>();

            // 5-向全局容器中注册视图窗体[ViewWindows]
            service?.AddSingleton(typeof(SettingStationView));
            service?.AddSingleton(typeof(SettingTargetView));
            service?.AddSingleton(typeof(SettingDeviceSlaveView));
            service?.AddSingleton(typeof(SettingDeviceChannelView));
            service?.AddSingleton(typeof(SettingUserRoleView));
            service?.AddSingleton(typeof(SettingRolePowerView));
            service?.AddSingleton(typeof(SettingSystemView));
            service?.AddSingleton(typeof(SettingView));
            service?.AddSingleton(typeof(ParameterView));
            service?.AddSingleton(typeof(DetectTargetView));
            service?.AddSingleton(typeof(DetectRecordView));
            service?.AddSingleton(typeof(MainPageView));
            service?.AddSingleton(typeof(MainWindow));
            service?.AddTransient(typeof(LoginWindow));

            // 6-再构建容器服务对象
            ContainerManager.Instance.BuildService();
        }
    }
}
